package com.doit.spark.day04

import com.doit.spark.day01.utils.SparkUtil
import org.apache.spark.rdd.RDD

/**
 * @DATE 2022/1/6/9:10
 * @Author MDK
 * @Version 2021.2.2
 *
 * 行动算子触发执行数据运算
 * */
object C01_行动算子02 {
  def main(args: Array[String]): Unit = {
    val sc = SparkUtil.getSc
    val ls =  List[Orders](
      Orders(1,"袜子",9.9) ,
      Orders(3,"裤子",99.9) ,
      Orders(5,"鞋子",299.9) ,
      Orders(2,"帽子",39.8) ,
      Orders(6,"褂子",339.9) ,
      Orders(4,"瓜子",8.9)
    )
    val rdd1: RDD[Orders] = sc.parallelize(ls, 2)
    // 隐式函数     方法的返回值是一个比较器
    implicit def myOrdering={
      new Ordering[Orders]{
        override def compare(x: Orders, y: Orders): Int = y.oid - x.oid
      }
    }

    //RDD 处理的数据封装可以使是可排序的
    //val arr: Array[Orders] = rdd1.top(3)//(myOrdering)
    val arr2: Array[Orders] = rdd1.takeOrdered(3)
    println(arr2.toList)
  }
}
